<?php

namespace app\api\service\wanlshop;
use think\Exception;
use think\Model;
use app\admin\model\User as UserModel;
use think\Validate;
use app\common\library\Auth;
class User extends Model
{
    // 用户模型
    protected $userModel = null;
    /**
     * 权限Auth
     * @var Auth
     */
    protected $auth = null;

    public function __construct($data = [])
    {
        parent::__construct($data);
        $this->userModel = new UserModel();
        $this->auth = Auth::instance();
    }

    /**
     *  转介绍
     */

    public function myIntroduceService($uid)
    {
        $response = $this->userModel->getMyIntroduce($uid);
        return $response;
    }

    /**
     *  合作伙伴
     */

    public function myPartnerService($uid, $page, $pageSize)
    {

        $response = $this->userModel->getPartnerList($uid, $page, $pageSize);
        return $response;

    }

    /**
     * h5注册
     */

    public function registerByh5Service($params)
    {

        // 1. 检测参数的合法性
        if (!Validate::regex($params['mobile'], "^1\d{10}$")) {
            throw new Exception('手机号不正确', 4);
        }

        $rule = [
            'mobile' => 'require',
            'password' => 'require',
            'invitation_code' => 'require',
        ];

        $msg = [
            'mobile.require' => '手机号不能为空',
            'password.require' => '密码不存在或不能为空',
            'invitation_code.require' => '邀请码不存在或不能为空'
        ];

        $v = new Validate($rule, $msg);
        if(!$v->check($params)){
            throw new Exception($v->getError(), 5);
        }

        // 注册
        $ret = $this->auth->register($params['mobile'], $params['password'], '', $params['mobile'], ['invitation_code'=>$params['invitation_code']]);

        if(!$ret){
            throw new Exception($this->auth->getError(), 2);
        }

        return $this->auth->getUserinfo();

    }
}
